package com.lyouu.dataminer.dao;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.List;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import com.lyouu.dataminer.db.DbConstants;
import com.lyouu.dataminer.factory.DaoFactory;
import com.lyouu.dataminer.factory.MysqlDbFactory;
import com.lyouu.dataminer.util.Constants;
import com.lyouu.dataminer.util.Util;

public class BustourScheduleDao {
	private Logger logger = Logger.getLogger(BustourScheduleDao.class);

	public void insert(String provider, String tourName,
			List<String> groupIdList, List<String> departDateList,
			List<String> endDateList, String currency,
			List<String> adultPriceList, List<String> childPriceList,
			List<String> groupPriceList, List<String> statusList,
			List<String> bookingList, String pageUrl) throws SQLException {
		int uid = DaoFactory.getMdMembersDao().getUidByUsername(provider);
		int sid = DaoFactory.getMdSubjectBustourDao().getSidByOrigPageUrl(
				pageUrl);
		Connection conn = MysqlDbFactory.getConnection(DbConstants.DB_LYOUU);
		String sql = "INSERT INTO ly_bustour_schedule"
				+ "(sid,provider,uid,tourname,groupid,departdate,enddate,duration,quota,signed,price,preprice,childprice,5groupprice,priceunit,status,booking,pageUrl)"
				+ " VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		PreparedStatement stmt = conn.prepareStatement(sql);
		int i;
		String[] date;
		for (i = 0; i < departDateList.size(); i++) {
			stmt.setInt(1, sid);// sid
			stmt.setString(2, provider);// provider
			stmt.setInt(3, uid);// uid
			stmt.setString(4, tourName);// tourname
			stmt.setString(5, groupIdList.get(i));
			date = departDateList.get(i).split("\\.");
			stmt.setDate(
					6,
					new Date(Integer.valueOf(date[0]) - 1900, Integer
							.valueOf(date[1]) - 1, Integer.valueOf(date[2])));
			date = endDateList.get(i).split("\\.");
			stmt.setDate(
					7,
					new Date(Integer.valueOf(date[0]) - 1900, Integer
							.valueOf(date[1]) - 1, Integer.valueOf(date[2])));
			stmt.setInt(8, Util.calcDuration(departDateList.get(i),
					endDateList.get(i)));// duration
			stmt.setInt(9, 0);// quota
			stmt.setInt(10, 0);// signed
			stmt.setBigDecimal(11,
					BigDecimal.valueOf(Double.valueOf(adultPriceList.get(i))));// price
			stmt.setBigDecimal(12, BigDecimal.ZERO);// preprice
			// childprice
			if (childPriceList.get(i).equals(Constants.UNKNOWN_PRICE))
				stmt.setBigDecimal(13, BigDecimal.ZERO);
			else
				stmt.setBigDecimal(13, BigDecimal.valueOf(Double
						.valueOf(childPriceList.get(i))));
			// 5groupprice
			if (groupPriceList.get(i).equals(Constants.UNKNOWN_PRICE))
				stmt.setBigDecimal(14, BigDecimal.ZERO);
			else
				stmt.setBigDecimal(14, BigDecimal.valueOf(Double
						.valueOf(groupPriceList.get(i))));
			stmt.setString(15, currency);
			stmt.setInt(16, getStatus(statusList.get(i)));// status
			stmt.setString(17, bookingList.get(i));// booking
			stmt.setString(18, pageUrl);// pageurl
			stmt.executeUpdate();
		}
		conn.close();
	}

	private int getStatus(String statusStr) {
		// kaiyuan_de
		if (Pattern.matches("\\d{1,2}/\\d{1,2}", statusStr)) {
			String[] statusAry = statusStr.split("/");
			if (Integer.valueOf(statusAry[1]) >= Integer.valueOf(statusAry[0]))
				return Constants.TOUR_GROUP_STATUS_FULL;
			if (Integer.valueOf(statusAry[1]) >= (Integer.valueOf(statusAry[0]) + 1) / 2)
				return Constants.TOUR_GROUP_STATUS_CONFIRMED;
			if (Integer.valueOf(statusAry[1]) > 5)
				return Constants.TOUR_GROUP_STATUS_READY;
		}
		// dehua_de,caissa_de
		else {
			if (statusStr.contains("准备发团") || statusStr.contains("准备出发"))
				return Constants.TOUR_GROUP_STATUS_READY;
			if (statusStr.contains("确定发团") || statusStr.contains("确定出发")
					|| statusStr.contains("欢迎预订"))
				return Constants.TOUR_GROUP_STATUS_CONFIRMED;
			if (statusStr.contains("结团") || statusStr.contains("超额报名"))
				return Constants.TOUR_GROUP_STATUS_FULL;

		}
		return Constants.TOUR_GROUP_STATUS_UNKNOWN;
	}
}
